package com.bjfu.gmms;

import com.bjfu.gmms.model.ExamScore;
import com.bjfu.gmms.pojo.*;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.mapping.MappedStatement;
import java.sql.Connection;
import java.sql.Statement;
import java.util.Properties;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@Intercepts({
        @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class })
})
public class SqlLoggerInterceptor implements Interceptor {

    private static final Logger logger = LogManager.getLogger(SqlLoggerInterceptor.class);

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        // 获取 StatementHandler 对象
        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        String sql = statementHandler.getBoundSql().getSql();  // 获取 SQL 语句
        logger.info("执行的 SQL: " + sql);

        // 获取 SQL 参数
        Object parameterObject = statementHandler.getBoundSql().getParameterObject();
        if (parameterObject != null) {
            if (parameterObject instanceof Object[]) {
                // 如果参数是 Object 数组
                Object[] params = (Object[]) parameterObject;
                for (Object param : params) {
                    logger.info("SQL 参数: " + param.toString());
                }
            } else if (parameterObject instanceof List) {
                // 如果参数是 List
                List<?> params = (List<?>) parameterObject;
                for (Object param : params) {
                    logger.info("SQL 参数: " + param);
                }
            } else {
                // 如果是单个对象
                logger.info("SQL 参数: " + parameterObject.toString());
                /*if (parameterObject instanceof ExamScore) {
                    ExamScore examScore = (ExamScore) parameterObject;
                    logger.info("SQL 参数: studentId = " + examScore.getStudentId());
                    logger.info("SQL 参数: examId = " + examScore.getExamId());
                    logger.info("SQL 参数: score = " + examScore.getScore());
                } else if (parameterObject instanceof ApplicationPreferenceInfo) {
                    ApplicationPreferenceInfo preferenceInfo = (ApplicationPreferenceInfo) parameterObject;
                    logger.info("SQL 参数: studentId = " + preferenceInfo.getStudentId());
                    logger.info("SQL 参数: mentorId = " + preferenceInfo.getMentorId());
                    logger.info("SQL 参数: researchId = " + preferenceInfo.getResearchId());
                    logger.info("SQL 参数: preferenceOrder = " + preferenceInfo.getPreferenceOrder());
                    logger.info("SQL 参数: preferenceStatus = " + preferenceInfo.getPreferenceStatus());
                } else if (parameterObject instanceof CollegeInfo) {
                    CollegeInfo collegeInfo = (CollegeInfo) parameterObject;
                    logger.info("SQL 参数: collegeId = " + collegeInfo.getCollegeId());
                    logger.info("SQL 参数: admissionCount = " + collegeInfo.getAdmissionCount());
                    logger.info("SQL 参数: recommendationCount = " + collegeInfo.getRecommendationCount());
                } else if (parameterObject instanceof CollegeStaticInfo) {
                    CollegeStaticInfo collegeStaticInfo = (CollegeStaticInfo) parameterObject;
                    logger.info("SQL 参数: collegeId = " + collegeStaticInfo.getCollegeId());
                    logger.info("SQL 参数: collegeName = " + collegeStaticInfo.getCollegeName());
                    logger.info("SQL 参数: collegeDetails = " + collegeStaticInfo.getCollegeDetails());
                } else if (parameterObject instanceof DisciplineExamInfo) {
                    DisciplineExamInfo disciplineExamInfo = (DisciplineExamInfo) parameterObject;
                    logger.info("SQL 参数: secondaryDisciplineId = " + disciplineExamInfo.getSecondaryDisciplineId());
                    logger.info("SQL 参数: examId = " + disciplineExamInfo.getExamId());
                } else if (parameterObject instanceof ExamInfo) {
                    ExamInfo examInfo = (ExamInfo) parameterObject;
                    logger.info("SQL 参数: examId = " + examInfo.getExamId());
                    logger.info("SQL 参数: examName = " + examInfo.getExamName());
                    logger.info("SQL 参数: examType = " + examInfo.getExamType());
                    logger.info("SQL 参数: examDetails = " + examInfo.getExamDetails());
                } else if (parameterObject instanceof MentorDirection) {
                    MentorDirection mentorDirection = (MentorDirection) parameterObject;
                    logger.info("SQL 参数: mentorId = " + mentorDirection.getMentorId());
                    logger.info("SQL 参数: researchId = " + mentorDirection.getResearchId());
                } else if (parameterObject instanceof MentorInfo) {
                    MentorInfo mentorInfo = (MentorInfo) parameterObject;
                    logger.info("SQL 参数: mentorId = " + mentorInfo.getMentorId());
                    logger.info("SQL 参数: mentorTittle = " + mentorInfo.getMentorTittle());
                    logger.info("SQL 参数: mentorQualification = " + mentorInfo.getMentorQualification());
                    logger.info("SQL 参数: allocateQuota = " + mentorInfo.getAllocateQuota());
                    logger.info("SQL 参数: admissionConfirmed = " + mentorInfo.getAdmissionConfirmed());
                } else if (parameterObject instanceof MentorStaticInfo) {
                    MentorStaticInfo mentorStaticInfo = (MentorStaticInfo) parameterObject;
                    logger.info("SQL 参数: mentorId = " + mentorStaticInfo.getMentorId());
                    logger.info("SQL 参数: mentorName = " + mentorStaticInfo.getMentorName());
                    logger.info("SQL 参数: mentorImg = " + mentorStaticInfo.getMentorImg());
                    logger.info("SQL 参数: mentorDetails = " + mentorStaticInfo.getMentorDetails());
                    logger.info("SQL 参数: mentorMail = " + mentorStaticInfo.getMentorMail());
                    logger.info("SQL 参数: mentorPhone = " + mentorStaticInfo.getMentorPhone());
                } else if (parameterObject instanceof PrimaryDisciplineInfo) {
                    PrimaryDisciplineInfo primaryDisciplineInfo = (PrimaryDisciplineInfo) parameterObject;
                    logger.info("SQL 参数: primaryDisciplineId = " + primaryDisciplineInfo.getPrimaryDisciplineId());
                    logger.info("SQL 参数: collageId = " + primaryDisciplineInfo.getCollageId());
                    logger.info("SQL 参数: name = " + primaryDisciplineInfo.getName());
                } else if (parameterObject instanceof ResearchDirectionInfo) {
                    ResearchDirectionInfo researchDirectionInfo = (ResearchDirectionInfo) parameterObject;
                    logger.info("SQL 参数: researchId = " + researchDirectionInfo.getResearchId());
                    logger.info("SQL 参数: secondaryDisciplineId = " + researchDirectionInfo.getSecondaryDisciplineId());
                    logger.info("SQL 参数: researchName = " + researchDirectionInfo.getResearchName());
                } else if (parameterObject instanceof RetestTable) {
                    RetestTable retestTable = (RetestTable) parameterObject;
                    logger.info("SQL 参数: studentId = " + retestTable.getStudentId());
                    logger.info("SQL 参数: secondaryDisciplineId = " + retestTable.getSecondaryDisciplineId());
                    logger.info("SQL 参数: isAdjustmentAccepted = " + retestTable.getIsAdjustmentAccepted());
                    logger.info("SQL 参数: passCode = " + retestTable.getPassCode());
                } else if (parameterObject instanceof SecondaryDisciplineInfo) {
                    SecondaryDisciplineInfo secondaryDisciplineInfo = (SecondaryDisciplineInfo) parameterObject;
                    logger.info("SQL 参数: secondaryDisciplineId = " + secondaryDisciplineInfo.getSecondaryDisciplineId());
                    logger.info("SQL 参数: quota = " + secondaryDisciplineInfo.getQuota());
                    logger.info("SQL 参数: additionalQuota = " + secondaryDisciplineInfo.getAdditionalQuota());
                    logger.info("SQL 参数: remarks = " + secondaryDisciplineInfo.getRemarks());
                } else if (parameterObject instanceof SecondaryDisciplineStaticInfo) {
                    SecondaryDisciplineStaticInfo secondaryDisciplineStaticInfo = (SecondaryDisciplineStaticInfo) parameterObject;
                    logger.info("SQL 参数: secondaryDisciplineId = " + secondaryDisciplineStaticInfo.getSecondaryDisciplineId());
                    logger.info("SQL 参数: primaryDisciplineId = " + secondaryDisciplineStaticInfo.getPrimaryDisciplineId());
                    logger.info("SQL 参数: name = " + secondaryDisciplineStaticInfo.getName());
                    logger.info("SQL 参数: details = " + secondaryDisciplineStaticInfo.getDetails());
                    logger.info("SQL 参数: type1 = " + secondaryDisciplineStaticInfo.getType1());
                    logger.info("SQL 参数: type2 = " + secondaryDisciplineStaticInfo.getType2());
                } else if (parameterObject instanceof StudentInfo) {
                    StudentInfo studentInfo = (StudentInfo) parameterObject;
                    logger.info("SQL 参数: studentId = " + studentInfo.getStudentId());
                    logger.info("SQL 参数: name = " + studentInfo.getName());
                    logger.info("SQL 参数: gender = " + studentInfo.getGender());
                    logger.info("SQL 参数: category1 = " + studentInfo.getCategory1());
                    logger.info("SQL 参数: category2 = " + studentInfo.getCategory2());
                    logger.info("SQL 参数: undergraduateSchool = " + studentInfo.getUndergraduateSchool());
                    logger.info("SQL 参数: graduationDate = " + studentInfo.getGraduationDate());
                    logger.info("SQL 参数: undergraduateMajor = " + studentInfo.getUndergraduateMajor());
                    logger.info("SQL 参数: phone = " + studentInfo.getPhone());
                    logger.info("SQL 参数: emergencyPhone = " + studentInfo.getEmergencyPhone());
                    logger.info("SQL 参数: birthDate = " + studentInfo.getBirthDate());
                    logger.info("SQL 参数: idNumber = " + studentInfo.getIdNumber());
                    logger.info("SQL 参数: sourceLocation = " + studentInfo.getSourceLocation());
                    logger.info("SQL 参数: mail = " + studentInfo.getMail());
                    logger.info("SQL 参数: undergraduateSchoolType = " + studentInfo.getUndergraduateSchoolType());
                    logger.info("SQL 参数: personalResume = " + studentInfo.getPersonalResume());
                    logger.info("SQL 参数: originalAppliedDiscipline = " + studentInfo.getOriginalAppliedDiscipline());
                } else if (parameterObject instanceof StudentMentorDirection) {
                    StudentMentorDirection studentMentorDirection = (StudentMentorDirection) parameterObject;
                    logger.info("SQL 参数: studentId = " + studentMentorDirection.getStudentId());
                    logger.info("SQL 参数: mentorId = " + studentMentorDirection.getMentorId());
                    logger.info("SQL 参数: researchId = " + studentMentorDirection.getResearchId());
                } else {
                    logger.info("SQL 参数: " + parameterObject);
                }*/

            }
        } else {
            logger.info("SQL 参数: null");
        }

        // 执行 SQL 语句
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);  // 创建代理对象
    }

    @Override
    public void setProperties(Properties properties) {
        // 获取配置的日志级别
        String logLevel = properties.getProperty("logLevel");
        System.out.println("配置的 logLevel: " + logLevel);  // 例如 "DEBUG"
    }
}
